package linwen.wang.redis.dao;


import com.alibaba.fastjson.JSON;
import linwen.wang.redis.vo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @ClassName TestRedisTemplate
 * @Description
 * @Author lin
 * @Date 2022/3/3 上午12:35
 * @Version V1.0
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestRedisTemplate {

    @Autowired
    RedisTemplate redisTemplate;

    // 使用fastjson自己转换对象
    @Test
    public void test(){
        User user = User.builder().name("zhangsan").age(18).address("shenzhen").gender("男").phone("18376662375")
                .build();
        // 使用fastjson将对象转换成字符串
        String jsonString = JSON.toJSONString(user);
        redisTemplate.opsForValue().set("user",jsonString);
        System.out.println(redisTemplate.opsForValue().get("user"));
    }
    // 默认方式直接存对象  会报序列话异常
    // 需要对象实现Serializable接口，jdk实现序列化
    // 使用自定义redisTemplate，实现序列话key value
    @Test
    public void test1(){
        User user = User.builder().name("zhangsan").age(18).address("shenzhen").gender("男").phone("18376662375")
                .build();
        redisTemplate.opsForValue().set("user",user);
        System.out.println(redisTemplate.opsForValue().get("user"));
    }

    @Test
    public void test2(){
        Long increatKey = redisTemplate.opsForValue().increment("increatKey");
        System.out.println(increatKey);
        Long increatKey1 = redisTemplate.opsForValue().increment("increatKey", 2);
        System.out.println(increatKey1);
        Long init = redisTemplate.opsForValue().increment("init3",10);
        System.out.println(init);
    }

}
